package com.sdruixinggroup.mondayb2b.ui.fragments;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.sdruixinggroup.mondayb2b.R;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/12/11.
 */

public class NongPayFragment extends BaseFragment {
    @BindView(R.id.ib_back)
    ImageView ibBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_begin_time)
    TextView tvBeginTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    Unbinder unbinder;
    @BindView(R.id.ll_click)
    LinearLayout llClick;
    private TimePickerView pvTime;
    private String time;

    @Override
    protected int getInflateLayout() {
        return R.layout.nong_pay_fragment;
    }

    @Override
    protected void setUpView(View view) {
        tvTitle.setText("应付账款支付");
        CharSequence beginTime = DateFormat.format("yyyy.MM.dd", System.currentTimeMillis());
        tvBeginTime.setText("开始日期  " + beginTime);

        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_end_time, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_end_time:
                initTimePicker();
                break;
            case R.id.tv_commit:
                onClick();
                break;
        }
    }

    private void initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11

        Calendar startDate = Calendar.getInstance();

        Calendar endDate = Calendar.getInstance();
        endDate.set(2100, 12, 31);
        //时间选择器
        pvTime = new TimePickerView.Builder(getContext(), new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null

                /*btn_Time.setText(getTime(date));*/
                tvEndTime.setText("结束日期  " + DateFormat.format("yyyy.MM.dd", date.getTime()));
                time = String.valueOf(DateFormat.format("yyyy-MM-dd", date.getTime()));
            }
        })
                .setType(TimePickerView.Type.YEAR_MONTH_DAY)
                .setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
                .setDividerColor(Color.DKGRAY)
                .setContentSize(20)
                .setDate(startDate)
                .setRangDate(startDate, endDate)
                .build();

        pvTime.show();
    }

    @OnClick(R.id.ll_click)
    public void onClick() {
        if (!TextUtils.isEmpty(time)) {
            Intent intent = new Intent();
            intent.putExtra("value", time);
            getActivity().setResult(Activity.RESULT_OK, intent);
            getActivity().finish();
        }else {
            showMsgToast("请选择结束时间");
        }
    }
}
